/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package WebServiceLogin;
import WebServiceUserModel.*;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;

/**
 *
 * @author micheal
 */
public class UserController {
    
    private static UserController user=new UserController();
    private static Connection con=null;
    private static PreparedStatement stmt=null;        
    private static String Driver="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://localhost:3306/GameUser";
    private static String gameuser="root";
    private static String password="586088";
    private static String sql;
    
    public UserController(){}
    
    public static UserController getInstance(){
        
        //DataBaseInit();    
        return user;
    }
    
    public static void DataBaseInit(){
        
        Statement stmt=null;
        
         try{ 

            Class.forName(Driver); 

        } 

        catch(Exception e){ 

            System.out.println("无法加载驱动程序" +Driver); 

        } 

        try{           
 
            con=DriverManager.getConnection(url,gameuser,password); 
            stmt=con.createStatement();
            sql="create table gameuser(ID varchar(16),Password varchar(14),Name varchar(12),Email varchar(14),Question varchar(20),Answer varchar(20),Address varchar(40),Detail varchar(8))";            
            stmt.execute(sql);           

            if(!con.isClosed()) 

                System.out.println("Connect successfully"); 

            con.close(); 

        } 
        catch(SQLException ee){ 
           System.out.println(ee);
        }    
    }
    
    public void AddUser(User entity){
        
        try{ 

            Class.forName(Driver); 

        } 

        catch(Exception e){ 

            System.out.println("无法加载驱动程序" +Driver); 

        } 

        try{           
 
            con=DriverManager.getConnection(url,gameuser,password); 
            
            sql="insert into gameuser (ID,Password,Name,Email,Question,Answer,Address,Detail) values (?,?,?,?,?,?,?,?)";             
            
            stmt=con.prepareStatement(sql);
            
            stmt.setString(1, entity.getID());
            stmt.setString(2, entity.getPassword());
            stmt.setString(3, entity.getName());
            stmt.setString(4, entity.getEmail());
            stmt.setString(5, entity.getQualifiedQuestion());
            stmt.setString(6, entity.getQualifiedAnswer());
            stmt.setString(7, entity.getAddress());
            stmt.setString(8, entity.getDetail());
                  
            stmt.executeUpdate();           

            if(!con.isClosed()) 

                System.out.println("添加成功"); 

            con.close(); 

        } 
        catch(SQLException ee){ 
           System.out.println("添加失败");
        }
        
    }
    
    public void DeleteUser(User entity){
        
        try{ 

            Class.forName(Driver); 

        } 

        catch(Exception e){ 

            System.out.println("无法加载驱动程序" +Driver); 

        } 

        try{           
 
            con=DriverManager.getConnection(url,gameuser,password); 
            
            sql="delete from gameuser where ID=?";             
            
            stmt=con.prepareStatement(sql);
            
            stmt.setString(1, entity.getID());                 
            
            stmt.executeUpdate();           
   
            if(!con.isClosed()) 

                System.out.println("删除成功"); 

            con.close(); 

        } 
        catch(SQLException ee){             
           System.out.println("删除失败");
        }
    
    }
    
    public void ModifyUser(User entity){
        
        try{ 

            Class.forName(Driver); 

        } 

        catch(Exception e){ 

            System.out.println("无法加载驱动程序" +Driver); 

        } 

        try{           
 
            con=DriverManager.getConnection(url,gameuser,password); 
            
            sql="update gameuser set Password=? ,Name=? ,Email=? ,Question=? ,Answer=? ,Address=? ,Detail=? where ID=?";             
            
            stmt=con.prepareStatement(sql);            
            
            stmt.setString(1, entity.getPassword());
            stmt.setString(2, entity.getName());
            stmt.setString(3, entity.getEmail());
            stmt.setString(4, entity.getQualifiedQuestion());
            stmt.setString(5, entity.getQualifiedAnswer());
            stmt.setString(6, entity.getAddress());
            stmt.setString(7, entity.getDetail());
            stmt.setString(8, entity.getID());
                  
            stmt.executeUpdate();          

            if(!con.isClosed()) 

                System.out.println("修改成功"); 

            con.close(); 

        } 
        catch(SQLException ee){            
            
           System.out.println("修改失败");
        }
    
    }
    
    public void setDetail(User entity){
        
        try{ 

            Class.forName(Driver); 

        } 

        catch(Exception e){ 

            System.out.println("无法加载驱动程序" +Driver); 

        } 

        try{           
 
            con=DriverManager.getConnection(url,gameuser,password); 
            
            sql="update gameuser set Detail=? where ID=?";             
            
            stmt=con.prepareStatement(sql);            
            
            stmt.setString(1, entity.getDetail());
            stmt.setString(2, entity.getID());
                  
            stmt.executeUpdate();          

            if(!con.isClosed()) 

                System.out.println("修改成功"); 

            con.close(); 

        } 
        catch(SQLException ee){            
            
           System.out.println("修改失败");
        }
    
    }
    
    public void releaseDetail(User entity){
    
        try{ 

            Class.forName(Driver); 

        } 

        catch(Exception e){ 

            System.out.println("无法加载驱动程序" +Driver); 

        } 

        try{           
 
            con=DriverManager.getConnection(url,gameuser,password); 
            
            sql="update gameuser set Detail=? where ID=?";             
            
            stmt=con.prepareStatement(sql);            
            
            stmt.setString(1, "0");
            stmt.setString(2, entity.getID());
                  
            stmt.executeUpdate();          

            if(!con.isClosed()) 

                System.out.println("修改成功"); 

            con.close(); 

        } 
        catch(SQLException ee){            
            
           System.out.println("修改失败");
        }
    
    }
    
    public User getDetail(User entity){
    
        ResultSet rs=null;        
        
        try{ 

            Class.forName(Driver); 

        } 

        catch(Exception e){ 

            System.out.println("无法加载驱动程序" +Driver); 

        } 

        try{           
 
            con=DriverManager.getConnection(url,gameuser,password); 
            
            sql="select Detail from gameuser where ID=?";             
            
            stmt=con.prepareStatement(sql);              
            
            stmt.setString(1, entity.getID());
                  
            rs=stmt.executeQuery(); 
            
            
            
            if(rs.next()){
                
                entity.SetDetail(rs.getString("Detail"));
            }
            
            rs.close();

            if(!con.isClosed()) 

                System.out.println("查询成功"); 

            con.close(); 
            
            return entity;
        } 
        catch(SQLException ee){             
           System.out.println("查询失败");
        }
        
        return null;
        
    }
    
    public boolean CheckUser(User entity){
        
        ResultSet rs=null;
        boolean Has=false;
        
        try{ 

            Class.forName(Driver); 

        } 

        catch(Exception e){ 

            System.out.println("无法加载驱动程序" +Driver); 

        } 

        try{           
 
            con=DriverManager.getConnection(url,gameuser,password); 
            
            sql="select ID from gameuser where ID=?";             
            
            stmt=con.prepareStatement(sql);              
            
            stmt.setString(1, entity.getID());
                  
            rs=stmt.executeQuery(); 
            
            
            
            if(rs.next()){
                Has=true;            
            }else{
            
                Has=false;
            }
            
            rs.close();

            if(!con.isClosed()) 

                System.out.println("查询成功"); 

            con.close(); 
        } 
        catch(SQLException ee){             
           System.out.println("查询失败");
        }
        
        return Has;
    
    }

    /*
    public static void main(String[] args){
        //DataBaseInit();
        UserController init=new UserController();
        for(int i=0;i<=10;i++){
            User user=User.getInstance();
            user.SetAddress(i+"");
            user.SetDetail(i+"");
            user.SetEmail(i+"");
            user.SetID(i+"");
            user.SetName(i+"");
            user.SetPassword(i+"");
            user.SetQualifiedAnswer(i+"");
            user.SetQualifiedQuestion(i+"");
            init.AddUser(user);
        }
    }
    */
}
